package cn.tianyigps.obd.channels;

import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.internal.SocketUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.nio.channels.SocketChannel;
import java.util.List;

/**
 * Created with IDEA
 *
 * @description: TCP服务通道，管道工厂类
 * @author: cui.guibin
 * @create: 2019/3/6 15:24
 **/
public class TYNioServerSocketChannel extends NioServerSocketChannel {

    private static final Logger logger = LogManager.getLogger(TYNioServerSocketChannel.class);

    @Override
    protected int doReadMessages(List<Object> buf) throws Exception {
        SocketChannel ch = SocketUtils.accept(this.javaChannel());
        logger.info("打印日志");
        try {
            if (ch != null) {
                buf.add(new TYNioSocketChannel(this, ch));
                return 1;
            }
        } catch (Throwable var6) {
            logger.warn("Failed to create a new channels from an accepted socket.", var6);

            try {
                ch.close();
            } catch (Throwable var5) {
                logger.warn("Failed to close a socket.", var5);
            }
        }

        return 0;
    }
}
